package com.sfh.dog.ui;

import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.sfh.dog.R;
import com.sfh.dog.dialog.Com;
import com.sfh.dog.dialog.DogCheckDialog;
import com.sfh.dog.entity.Dog;
import com.sfh.dog.util.StringUtils;
import com.sfh.dog.util.ViewUtil;

public abstract class ADogActivity extends APhotoFileActivity
{
    public EditText etDogName, etSpecialty, etLove, etDisgusting;
    
    public TextView tvTitle, tvDogType1, tvDogType2, tvDogStatus, tvDogBirthday;
    
    public LinearLayout lySpecialty, lyLove, lyDisgusting;
    
    public RadioButton rbMM, rbGG;
    
    public ImageView ivDogphoto;
    
    public Dog dogEntity;
    
    // 特长
    public SparseArray<String> SpecialtyCheck = new SparseArray<String>();
    
    // 喜欢
    public SparseArray<String> LoveCheck = new SparseArray<String>();
    
    // 讨厌
    public SparseArray<String> DisgustingCheck = new SparseArray<String>();
    
    public View pBLoad;

    public String filePath = "";
    
    
    public abstract void init();
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_edit_dog_layout);
        find();
        setListern();
        init();
    }
    
    private void find()
    {
        // 退出
        findViewById(R.id.ivLeft).setOnClickListener(new View.OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                finish();
            }
        });
        pBLoad = findViewById(R.id.pBLoad);
        ivDogphoto = (ImageView)findViewById(R.id.ivDogphoto);
        tvTitle = (TextView)findViewById(R.id.tvTitle);
        tvDogType1 = (TextView)findViewById(R.id.tvDogType1);
        tvDogType2 = (TextView)findViewById(R.id.tvDogType2);
        tvDogStatus = (TextView)findViewById(R.id.tvDogStatus);
        tvDogBirthday = (TextView)findViewById(R.id.tvDogBirthday);
        
        etDogName = (EditText)findViewById(R.id.etDogName);
        etSpecialty = (EditText)findViewById(R.id.etSpecialty);
        etLove = (EditText)findViewById(R.id.etLove);
        etDisgusting = (EditText)findViewById(R.id.etDisgusting);
        lySpecialty = (LinearLayout)findViewById(R.id.lySpecialty);
        lyLove = (LinearLayout)findViewById(R.id.lyLove);
        lyDisgusting = (LinearLayout)findViewById(R.id.lyDisgusting);
        // 性别
        rbMM = ((RadioButton)findViewById(R.id.rbDogMM));
        rbGG = ((RadioButton)findViewById(R.id.rbDogGG));
        
    }
    
    private void setListern()
    {
        // 保存
        findViewById(R.id.ivRight).setOnClickListener(mClickListener);
        findViewById(R.id.tvCheckPhoto).setOnClickListener(mClickListener);
        // 特长
        findViewById(R.id.tvSpecialtyCheck).setOnClickListener(mClickListener);
        // 喜欢
        findViewById(R.id.tvLoveCheck).setOnClickListener(mClickListener);
        findViewById(R.id.tvDisgustingCheck).setOnClickListener(mClickListener);
        // 性别
        rbMM.setOnClickListener(mClickListener);
        rbGG.setOnClickListener(mClickListener);
        tvDogType1.setOnClickListener(mClickListener);
        tvDogType2.setOnClickListener(mClickListener);
        tvDogStatus.setOnClickListener(mClickListener);
        tvDogBirthday.setOnClickListener(mClickListener);
    }
    
    private OnClickListener mClickListener = new OnClickListener()
    {
        
        @Override
        public void onClick(View v)
        {
            if (v == tvDogBirthday)
            {
                Com.date(ADogActivity.this, new Com.IDialogData()
                {
                    @Override
                    public void selectdItem(String content)
                    {
                        tvDogBirthday.setText(content);
                    }
                }).show();
                
            }
            else if (v == tvDogStatus)
            {
                Com.date(ADogActivity.this, new String[] {"sss", "sss", "还好"}, new Com.IDialogItem()
                {
                    
                    @Override
                    public void selectdItem(String content)
                    {
                        tvDogType2.setText(content);
                    }
                }).show();
            }
            else if (v == tvDogType1)
            {
                // 种类
                Com.date(ADogActivity.this, new String[] {"狗狗", "猫咪", "其他"}, new Com.IDialogItem()
                {
                    
                    @Override
                    public void selectdItem(String content)
                    {
                        tvDogType1.setText(content);
                    }
                }).show();
            }
            else if (v == tvDogType2)
            {
                
            }
            else if (v == rbGG)
            {
                dogEntity.setSex(1);
            }
            else if (v == rbMM)
            {
                dogEntity.setSex(0);
            }
            else
            {
                
                switch (v.getId())
                {
                    case R.id.tvDisgustingCheck:
                    {
                        // 讨厌
                        new DogCheckDialog(ADogActivity.this, getResources().getStringArray(R.array.disgusting_check),
                            "宠宝讨厌的事情", DisgustingCheck, new DogCheckDialog.IDismiss()
                            {
                                @Override
                                public void close()
                                {
                                    lyDisgusting.removeAllViews();
                                    if (DisgustingCheck.size() > 0)
                                    {
                                        ViewUtil.setLove(ADogActivity.this, lyDisgusting, DisgustingCheck);
                                    }
                                }
                            }).show();
                        break;
                    }
                    case R.id.tvLoveCheck:
                    {
                        new DogCheckDialog(ADogActivity.this, getResources().getStringArray(R.array.love_check),
                            "宠宝喜欢的事情", LoveCheck, new DogCheckDialog.IDismiss()
                            {
                                
                                @Override
                                public void close()
                                {
                                    lyLove.removeAllViews();
                                    if (LoveCheck.size() > 0)
                                    {
                                        ViewUtil.setLove(ADogActivity.this, lyLove, LoveCheck);
                                    }
                                }
                            }).show();
                        break;
                    }
                    case R.id.tvSpecialtyCheck:
                    {
                        new DogCheckDialog(ADogActivity.this, getResources().getStringArray(R.array.specialty_check),
                            "宠物特长", SpecialtyCheck, new DogCheckDialog.IDismiss()
                            {
                                
                                @Override
                                public void close()
                                {
                                    lySpecialty.removeAllViews();
                                    if (SpecialtyCheck.size() > 0)
                                    {
                                        ViewUtil.setLove(ADogActivity.this, lySpecialty, SpecialtyCheck);
                                    }
                                }
                            }).show();
                        break;
                    }
                    
                    case R.id.tvCheckPhoto:
                    {
                        openPhotoLib();
                        //                        new SystemPictureDialog(AddDogActivity.this, new SystemPictureDialog.IPictrue()
                        //                        {
                        //                            
                        //                            @Override
                        //                            public void takePhoto()
                        //                            {
                        //                                openNewPhoto();
                        //                                
                        //                            }
                        //                            
                        //                            @Override
                        //                            public void openLib()
                        //                            {
                        //                                openPhotoLib();
                        //                            }
                        //                        }).show(v);
                        break;
                    }
                    
                    case R.id.ivRight:
                    {
                        // 保存
                        saveDog();
                        break;
                    }
                }
            }
        }
    };
    
    public abstract  void saveDog();
    public boolean isNull()
    {
        Dog entity = getDogEntityData();
        if (StringUtils.isNullOrEmpty(entity.getName()))
        {
            //  showToast("昵称不能为空");
            return true;
        }
        
        if (StringUtils.isNullOrEmpty(entity.getBirthday()))
        {
            //  showToast("出生不能为空");
            return true;
        }
        if (StringUtils.isNullOrEmpty(entity.getStatus()))
        {
            //  showToast("状态不能为空");
            return true;
        }
        
        if (StringUtils.isNullOrEmpty(entity.getTypeDog()))
        {
            //  showToast("品种不能为空");
            return true;
        }
        
        if (StringUtils.isNullOrEmpty(entity.getTypeName()))
        {
            //  showToast("品种名不能为空");
            return true;
        }
        
        return false;
    }
    
    public Dog getDogEntityData()
    {
        dogEntity.setName(etDogName.getText().toString());
        dogEntity.setTypeDog(tvDogType1.getText().toString());
        
        dogEntity.setTypeName(tvDogType2.getText().toString());
        dogEntity.setStatus(tvDogStatus.getText().toString());
        dogEntity.setBirthday(tvDogBirthday.getText().toString());
        dogEntity.setSpecialty(ViewUtil.getArray(SpecialtyCheck));
        String specialty = etSpecialty.getText().toString();
        
        if (!StringUtils.isNullOrEmpty(specialty))
        {
            dogEntity.setSpecialty(dogEntity.getSpecialty() + "," + specialty);
        }
        
        dogEntity.setLove(ViewUtil.getArray(LoveCheck));
        String love = etLove.getText().toString();
        if (!StringUtils.isNullOrEmpty(love))
        {
            dogEntity.setLove(dogEntity.getLove() + "," + love);
        }
        
        dogEntity.setDisgusting(ViewUtil.getArray(LoveCheck));
        String disgusting = etDisgusting.getText().toString();
        if (!StringUtils.isNullOrEmpty(disgusting))
        {
            dogEntity.setDisgusting(dogEntity.getDisgusting() + "," + disgusting);
        }
        return dogEntity;
    }
    
  
    
    @Override
    public void loadFilePath(String path)
    {
        this.filePath = path;
        ImageLoader.getInstance().displayImage("file://" + path, ivDogphoto);
    }
}
